package com.soft.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.soft.admin.dto.MenuDetailDTO;
import com.soft.admin.dto.UserCacheDTO;
import com.soft.admin.service.HomeService;
import com.soft.admin.service.impl.cache.HomeServiceCache;
import com.soft.model.User;
import com.soft.util.JsonUtil;
import com.soft.util.RequestUtil;

@SuppressWarnings("unchecked")
@Component
public class HomeServiceImpl extends BaseServiceImpl implements HomeService {
	
	private HomeServiceCache homeServiceCache;
	
	@Transactional
	public String get() {
		UserCacheDTO user = this.homeServiceCache.getUserByUsername(RequestUtil.getUsername());
		List<MenuDetailDTO> menus = this.homeServiceCache.getMenuByUser(user.getUsername());
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("userinfo", user);
		resMap.put("menus", menus);
		return JsonUtil.toStringFromObject(resMap);
	}
	
	@Transactional
	public String editUser(String password) {
		if (password.trim().length() < 5) {
			return JsonUtil.toRes("保存失败！", "密码不合法！");
		}
		User user = (User)this.getBaseDao().findUniqueByProperty(User.class, "username", RequestUtil.getUsername());
		user.setPassword(password);
		this.getBaseDao().saveOrUpdate(user);
		return JsonUtil.toRes("保存成功！");
	}

	@Autowired
	public void setHomeServiceCache(HomeServiceCache homeServiceCache) {
		this.homeServiceCache = homeServiceCache;
	}

}
